﻿using MYZSZ.Common;
using MYZSZ.DTO;
using MYZSZ.IService;
using MYZSZ.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MYZSZ.Service
{
   public class AdminUserService : IAdminUserService
    {
        public long AddAdminUser(string name, string phoneNum, string password, string email, long? cityId)
        {
            AdminUserEntity userEntity = new AdminUserEntity();
            userEntity.CityId = cityId;
            userEntity.Email = email;
            userEntity.Name = name;
            userEntity.PhoneNum = phoneNum;
            string salt = CommonHelper.CreateVerifyCode(5);//盐
            userEntity.PasswordSalt = salt;
            //md5(密码+盐)
            string passHash = CommonHelper.CalcMD5(password + salt);
            userEntity.PasswordHash = passHash;
            using (MYZSZDbContext ctx = new MYZSZDbContext())
            {
                BaseService<AdminUserEntity> bs = new BaseService<AdminUserEntity>(ctx);
                bool exists = bs.GetAll().Any(u => u.PhoneNum == phoneNum);
                if(exists)
                {
                    throw new ArgumentException("手机号已经存在"+phoneNum);
                }
                ctx.AdminUsers.Add(userEntity);
                ctx.SaveChanges();
                return userEntity.Id;

            }
        }

        public bool CheckLogin(string phoneNum, string password)
        {
            throw new NotImplementedException();
        }

        public AdminUserDTO[] GetAll(long? cityId)
        {
            throw new NotImplementedException();
        }

        public AdminUserDTO[] GetAll()
        {
            throw new NotImplementedException();
        }

        public AdminUserDTO GetById(long id)
        {
            throw new NotImplementedException();
        }

        public AdminUserDTO GetByPhoneNum(string phoneNum)
        {
            throw new NotImplementedException();
        }

        public bool HasPermission(long adminUserId, string permissionName)
        {
            throw new NotImplementedException();
        }

        public void MarkDeleted(long adminUserId)
        {
            throw new NotImplementedException();
        }

        public void RecordLoginError(long id)
        {
            throw new NotImplementedException();
        }

        public void ResetLoginError(long id)
        {
            throw new NotImplementedException();
        }

        public void UpdateAdminUser(long id, string name, string phoneNum, string password, string email, long? cityId)
        {
            throw new NotImplementedException();
        }
    }
}
